package com.ywwxhz.app.fragment;

import android.os.Bundle;
import android.os.Environment;
import android.preference.Preference;
import android.view.View;
import android.widget.Toast;

import com.ywwxhz.Util.Config;
import com.ywwxhz.Util.FileKit;
import com.ywwxhz.Util.PreferencesKit;
import com.ywwxhz.taskmgr.BuildConfig;
import com.ywwxhz.taskmgr.R;

import java.io.File;

public class GeneralPreferenceFragment extends BasePreferenceFragment {

    private Preference restorePreference;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_general);
    }

    @Override
    protected int setSubtitle() {
        return R.string.pref_general;
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        findPreference(getString(R.string.pref_version_key)).setSummary(getVersionName());
        findPreference(getString(R.string.pref_ignorelist_key)).setOnPreferenceClickListener(this);
        findPreference(getString(R.string.pref_backup_key)).setOnPreferenceClickListener(this);
        findPreference(getString(R.string.pref_plugins_key)).setOnPreferenceClickListener(this);
        findPreference(getString(R.string.pref_laboratory_key)).setOnPreferenceClickListener(this);
        restorePreference = findPreference(getString(R.string.pref_restore_key));
        restorePreference.setOnPreferenceClickListener(this);
        File file = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/taskmgr_configure.xml");
        restorePreference.setEnabled(file.exists());
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        if (preference.getKey().equals(getString(R.string.pref_ignorelist_key))) {
            getFragmentManager().beginTransaction().replace(android.R.id.content, new IgnoreListFragment()).addToBackStack("current").commit();
        } else if (preference.getKey().equals(getString(R.string.pref_backup_key))) {
            long result;
            try {
                String srcFile = "/data/data/com.ywwxhz.taskmgr/shared_prefs/" + Config.CONFIG_NAME + ".xml";
                File destDir = Environment.getExternalStorageDirectory();
                result = FileKit.copyFile(srcFile, destDir, "taskmgr_configure.xml");
            } catch (Exception e) {
                result = -1;
            }
            if (result != -1) {
                Toast.makeText(getActivity(), "配置文件备份成功", Toast.LENGTH_SHORT).show();
                restorePreference.setEnabled(true);
            } else {
                Toast.makeText(getActivity(), "配置文件备份失败", Toast.LENGTH_SHORT).show();
            }
        } else if (preference.getKey().equals(getString(R.string.pref_restore_key))) {
            long result;
            try {
                String srcFile = Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + "/taskmgr_configure.xml";
                File destDir = new File(
                        "/data/data/com.ywwxhz.taskmgr/shared_prefs");
                result = FileKit.copyFile(srcFile, destDir,
                        Config.CONFIG_NAME + ".xml");
            } catch (Exception e) {
                result = -1;
            }
            if (result != -1) {
                PreferencesKit.writeLong(getActivity(), "batterytime", System.currentTimeMillis());
                Toast.makeText(getActivity(), "配置文件恢复成功，部分设置重启生效", Toast.LENGTH_SHORT)
                        .show();
            } else {
                Toast.makeText(getActivity(), "配置文件恢复失败", Toast.LENGTH_SHORT).show();
            }
        } else if (preference.getKey().equals(getString(R.string.pref_plugins_key))) {
            getFragmentManager().beginTransaction().replace(android.R.id.content, new PluginsPreferenceFragment()).addToBackStack("current").commit();
        } else if (preference.getKey().equals(getString(R.string.pref_laboratory_key))) {
            getFragmentManager().beginTransaction().replace(android.R.id.content, new LaboratoryPreferenceFragment()).addToBackStack("current").commit();
        }
        return true;
    }

    private String getVersionName() {
        return "Ver. " + BuildConfig.VERSION_NAME + " " + BuildConfig.BUILD_TYPE;
    }

    @Override
    public boolean getShouldExit() {
        return true;
    }
}